<?php

namespace app\forms\position;

use yii\db\Exception;
use app\forms\Model;
use app\models\Position;
use yii\web\HttpException;
use app\validators\PositiveInteger;

/**
 * 岗位 删除
 */
class DeleteForm extends Model
{
    public $id;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],
            ['id', 'required', 'message' => 'ID不为空'],
            ['id', 'exist', 'targetClass' => Position::class],
        ];
    }

    /**
     * @param $param
     * @throws HttpException
     * @throws \Throwable
     */
    public static function delete($param)
    {
        $form = new self();
        $form->setAttributes($param);
        !$form->validate() ? $form->validateError() : '';

        try {
            // 删除 岗位 及 岗位权限
            $model = Position::findOne($form['id']);
            $model->removeItem();
            $model->delete();

            // 是否要判断 岗位下有用户不删除  或  将用户的岗位重置为空

        } catch (Exception $e) {
            $form->serverError();
        }

        $form->deleteOk();
    }
}